#include <iostream>
using namespace std;

int find(int *f,int x) {
	if(f[x] == x) return x;
	return f[x] = find(f,f[x]);
}

int main() {
	int n, m, ans;
	cin>>n>>m;
	int f[n*3+5];
	for (int i=0; i<n*3; i++) { f[i] = i; }
	for (int i=0;i<m;i++){
		int k,x,y;
		cin>>k>>x>>y;
		if (x > n || y > n) {
			ans++;
			continue;
		}
		x--;
		y--;
		if (k == 1) {
			if (find(f,x + n) == find(f,y) || find(f,x) == find(f,y + n)) { ans++; }
			else {
				f[find(f,x)] = find(f,y);
				f[find(f,x + n)] = find(f,y + n);
				f[find(f,x + n + n)] = find(f,y + n + n);
			}
		}
		else {
			if (find(f,x) == find(f,y) || find(f,x) == find(f,y + n)) { ans++; }
			else {
				f[find(f,x + n)] = find(f,y);
				f[find(f,x + n + n)] = find(f,y + n);
				f[find(f,x)] = find(f,y + n + n);
			}
		}
	}
	cout<<ans;
	return 0;
}
